#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

int ReadDir()
{
	DIR *dir;
	struct dirent *d_stat;
	char file[512] = {0};
	char log[512] = {0};
	struct stat f_stat;
	char mode[12] = {0};
	unsigned char f_mode = 0;
	unsigned int mode_mask = 0;
	int i = 0, j = 0;
	static unsigned int total = 0;
	time_t f_time;
	char *pTime = NULL;
	const char *path = "/mnt/nandflash/bak";
	dir = opendir(path);
	if(!dir){
		printf("opendir %s failed \n",path);
		perror("openfile");
		return -1;
	}
	
	while(d_stat = readdir(dir)){
		if(!strcmp(d_stat->d_name, ".") || !strcmp(d_stat->d_name, "..")){
			continue;
		}
		
		memset(file, 0, sizeof(file));
		memset(&f_stat, 0, sizeof(f_stat));
		
		sprintf(file, "%s/%s",path, d_stat->d_name);
		printf("file : %s", file);
		stat(file, &f_stat);
		f_time = f_stat.st_mtime;
		time_t now_time = time(NULL);
		if((now_time - f_time) > (60 * 10)){
			remove(file);
		}
	}
	closedir(dir);
	
	return 0;
}


int catDF()
{
	char buf[512] = {0};
	FILE *fp = NULL;
    fp = popen("df | grep nandflash | awk '{print $5}'","r");
    if(NULL == fp){
        printf("popen failed \n");
    }else{
        fgets(buf, sizeof(buf), fp);
        pclose(fp);
        unsigned char percent = atoi(buf);
        printf("use flash  %d%% \n", percent);
		ReadDir();
    }

}

int main()
{
	//ReadDir();
	const char *buf = "123%";
	printf("atoi(%s) = %d \n", buf, atoi(buf));
}